/**
 * Created by ljg on 2017/3/27.
 */

const searchSelect=function () {
    angular.module('ui.table')
        .directive('searchSelectRemote', function (ajaxHelper) {
            return {
                restrict: 'E',
                replace: true,
                transclude: true,
                controller:function ($scope,$attrs) {
                    ajaxHelper.getJson($attrs.url,function (result) {
                        const dataList=result.data||[];
                        const valueName=$attrs.valueName||'code';
                        const textName=$attrs.textName||'name';
                        const placeholder=$attrs.placeholder?$attrs.placeholder:"请选择";
                        const options=[{code:"",name:placeholder}];
                        dataList.map(data=>{
                            const op={};
                            op.code=data[valueName];
                            op.name=data[textName];
                            options.push(op);
                        });
                        $scope.dataOptions=options;
                        $scope.$apply();
                        $($attrs.$$element).find('select').val($attrs.value);

                    });
                },
                scope:{},
                template: function(el,attr){
                    return '<div class="'+attr.class+'" >' +
                            '<select class="form-control" name='+attr.name+' >' +
                                '<option ng-repeat="op in dataOptions" value={{op.code}}>{{op.name}}</option>'+
                            '</select>' +
                            '</div>';
                },
                link : function(scope, element, attrs) {

                },
            };
        });
}
export default searchSelect;
